package cn.edu.zxj.trans.controller;

import cn.edu.zxj.trans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-02-03
 * @time: 12:58:06
 */
@RestController
@RequestMapping("/transactional")
public class TransactionalController {
    @Autowired
    private UserService userService;


    @Transactional // 注解方式进行事务回滚
    @RequestMapping("/registry")
    public String registry(String userName, String password) {
        // 用户注册
        userService.insert(userName, password);

        // 手动抛出异常
        try {
            int a = 1 / 0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return "用户注册成功";
    }
}
